package com.sanpu.kxe.business.login.view;

import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.login.adapter.WelcomePagerAdapter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.IntentTool;

import butterknife.Bind;

/**
 * Description：新引导页
 * Created by 薛勇军 on 2019/3/30.
 */

public class WelcomeNewActivity extends BaseTitleActivity {

    @Bind(R.id.viewpage)
    ViewPager vViewpage;
    @Bind(R.id.startRtv)
    RoundTextView startRtv;

    private WelcomePagerAdapter mWelcomePagerAdapter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_welcome_new;
    }

    @Override
    protected void initView() {
        super.initView();
        //取消第一次启动条件
        CacheManage.getInstance().saveCache(CacheModel.WELCOME, false);

        mWelcomePagerAdapter = new WelcomePagerAdapter(getSupportFragmentManager());
        vViewpage.setAdapter(mWelcomePagerAdapter);
    }

    @Override
    protected void setListener() {
        super.setListener();
        startRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentTool.startActivity(mContext, LoginActivity.class, true);
            }
        });
        vViewpage.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 1) {
                    timer.start();
                } else {
                    timer.cancel();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    CountDownTimer timer = new CountDownTimer(3 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
            IntentTool.startActivity(mContext, LoginActivity.class, true);
        }
    };


    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }
}
